<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="咨询公司" prop="companyId">
        <el-select
          v-model="queryParams.companyId"
          clearable
          @keyup.enter.native="handleQuery"
          filterable
          placeholder="请输入或选择咨询公司"
        >
          <el-option
            v-for="item in company"
            :key="item.companyId"
            :label="item.companyName"
            :value="item.companyId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="投诉人手机号"
        label-width="107px"
        prop="complainantPhone"
      >
        <el-input
          v-model="queryParams.complainantPhone"
          placeholder="请输入投诉人手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="投诉时间" prop="complainTime">
        <el-date-picker
          clearable
          v-model="queryParams.complainTime"
          type="date"
          value-format="yyyy-MM-dd"
          :style="{ width: '100%' }"
          placeholder="请选择投诉时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="被投诉人" prop="respondent">
        <el-input
          v-model="queryParams.respondent"
          placeholder="请输入被投诉人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="投诉标题" prop="complainTitle">
        <el-input
          v-model="queryParams.complainTitle"
          placeholder="请输入投诉标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="回复人" prop="userId">
        <el-select
          v-model="queryParams.userId"
          clearable
          @keyup.enter.native="handleQuery"
          filterable
          placeholder="请输入或选择回复人"
        >
          <el-option
            v-for="item in acceptor"
            :key="item.userId"
            :label="item.nickName"
            :value="item.userId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:complain:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:complain:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-badge :value="noReplyNumber" :max="10">
          <el-button
            size="mini"
            icon="el-icon-more"
            type="primary"
            plain
            @click="handleReply"
            >未回复</el-button
          >
        </el-badge>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-thumb"
          size="mini"
          :key="count"
          @click="handleAnonymous(count)"
          >匿名</el-button
        >
      </el-col>
      <audio
        controls="controls"
        hidden
        src="../voice/zhendong.mp3"
        ref="audio1"
      ></audio>
      <audio
        controls="controls"
        hidden
        src="../voice/tips.mp3"
        ref="audio2"
      ></audio>
      <audio
        controls="controls"
        hidden
        src="../voice/delete.mp3"
        ref="audio3"
      ></audio>
      <audio
        controls="controls"
        hidden
        src="../voice/Download.mp3"
        ref="audio4"
      ></audio>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="complainList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" width="55" />
      <el-table-column
        label="投诉公司"
        align="center"
        width="120"
        prop="companyName"
        :show-overflow-tooltip="true"
        @mouseenter="tip()"
        @mouseleave="leave()"
      >
        <template slot-scope="scope">
          {{ scope.row.anonymous == "1" ? "********" : scope.row.companyName }}
        </template>
      </el-table-column>
      <el-table-column
        label="投诉人手机号"
        align="center"
        prop="complainantPhone"
        width="107"
      >
        <template slot-scope="scope">
          {{
            scope.row.complainantPhone != null
              ? scope.row.complainantPhone.replace(
                  /^(.{3}).*(.{4})$/,
                  "$1****$2"
                )
              : null
          }}
        </template>
      </el-table-column>
      <el-table-column
        label="投诉时间"
        align="center"
        prop="complainTime"
        width="180"
        sortable
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.complainTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="被投诉人"
        align="center"
        prop="respondent"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="投诉标题"
        align="center"
        prop="complainTitle"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="投诉内容"
        align="center"
        prop="complainContent"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="回复人" align="center" prop="nickName" />
      <el-table-column
        label="回复内容"
        align="center"
        prop="replyContent"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="180px"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:complain:edit']"
            >回复</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:complain:remove']"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleLookOver(scope.row)"
            v-hasPermi="['system:advice:lookOver']"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="107px">
        <el-form-item label="是否匿名" prop="anonymous">
          <el-radio-group v-model="form.anonymous" size="small">
            <el-radio-button
              v-for="dict in dict.type.anonymous"
              :key="dict.value"
              :label="dict.value"
            >
              {{ dict.label }}
            </el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="投诉公司" prop="companyId">
          <el-select v-model="form.companyId" placeholder="请选择公司">
            <el-option
              v-for="item in company"
              :key="item.companyId"
              :label="item.companyName"
              :value="item.companyId"
              :disabled="item.fakeDelete == 0"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="投诉人手机号"
          label-width="107px"
          prop="complainantPhone"
        >
          <el-input
            v-model="form.complainantPhone"
            placeholder="请输入投诉人手机号"
          />
        </el-form-item>
        <el-form-item label="被投诉人" prop="respondent">
          <el-input v-model="form.respondent" placeholder="请输入被投诉人" />
        </el-form-item>
        <el-form-item label="投诉标题" prop="complainTitle">
          <el-input v-model="form.complainTitle" placeholder="请输入投诉标题" />
        </el-form-item>
        <el-form-item label="投诉内容" prop="complainContent">
          <el-input
            v-model="form.complainContent"
            :min-height="192"
            type="textarea"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 修改对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="opens"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="回复内容" prop="replyContent">
          <el-input
            v-model="form.replyContent"
            :min-height="192"
            type="textarea"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 查看投诉弹窗 -->
    <el-dialog
      :title="title"
      :visible.sync="lookOver"
      width="500px"
      class="maskpopup"
      append-to-body
    >
      <el-form ref="form" :model="form" label-width="120px" size="mini">
        <el-row>
          <el-col :span="22">
            <el-form-item label="投诉公司：">{{
              form.companyName
            }}</el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="投诉人手机号：">{{
              form.complainantPhone
            }}</el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="投诉时间：">{{
              form.complainTime
            }}</el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="被投诉人：">{{
              form.respondent
            }}</el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="投诉标题：">{{
              form.complainTitle
            }}</el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="投诉内容：">{{
              form.complainContent
            }}</el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="回复人：">{{ form.nickName }}</el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="回复内容：">{{
              form.replyContent
            }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  listComplain,
  getComplain,
  delComplain,
  addComplain,
  updateComplain,
  getNoReplyComplain,
  getAnonymous,
  getNoAnonymous
} from "@/api/system/complain";
import { companyList } from "@/api/system/company";
import { listUser } from "@/api/system/user";

export default {
  name: "Complain",
  dicts: ["anonymous"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //未回复消息数量
      noReplyNumber: 0,
      // 投诉表格数据
      complainList: [],
      // 弹出层标题
      title: "",
      // 匿名按钮点击次数
      count: 0,
      // 是否显示弹出层
      open: false,
      opens: false,
      lookOver: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        anonymous: null,
        companyId: null,
        companyName: null,
        complainantPhone: null,
        complainTime: null,
        respondent: null,
        complainTitle: null,
        complainContent: null,
        userId: null,
        nickName: null,
        replyContent: null,
        fakeDelete: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        anonymous: [
          {
            required: true,
            message: "是否匿名不能为空",
            trigger: "change",
          },
        ],
        companyId: [
          {
            required: true,
            message: "请选择下拉选择",
            trigger: "change",
          },
        ],
        complainantPhone: [
          {
            required: true,
            message: "请输入手机号",
            trigger: "blur",
          },
          {
            pattern: /^1(3|4|5|7|8|9)\d{9}$/,
            message: "手机号格式错误",
            trigger: "change",
          },
        ],
        respondent: [
          {
            required: true,
            message: "被投诉人不能为空",
            trigger: "change",
          },
          {
            pattern: /^[\u4E00-\u9FA5]{2,20}(·[\u4E00-\u9FA5]{2,20}){0,2}$/,
            message: "人名不符合规则",
            trigger: "change",
          },
        ],
        complainTitle: [
          {
            required: true,
            message: "投诉标题不能为空",
            trigger: "change",
          },
        ],
        complainContent: [
          {
            required: true,
            message: "投诉内容不能为空",
            trigger: "change",
          },
        ],
        replyContent: [
          {
            required: true,
            message: "回复内容不能为空",
            trigger: "change",
          },
        ],
      },
      //新增咨询时咨询公司下拉列表
      company: [],
      //回复人下拉列表
      acceptor: [],
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.getCompany();
    this.getAcceptor();
    this.handle();
  },
  methods: {
    //受理人列表
    getAcceptor() {
      listUser(this.queryParams).then((response) => {
        this.acceptor = response.rows;
      });
    },
    //公司列表
    getCompany() {
      companyList(this.queryParams).then((response) => {
        this.company = response.rows;
      });
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listComplain(this.queryParams).then((response) => {
        this.complainList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查看并回复未回复的消息 */
    handleReply() {
      this.loading = true;
      getNoReplyComplain(this.queryParams).then((response) => {
        this.complainList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handle() {
      getNoReplyComplain(this.queryParams).then((response) => {
        this.noReplyNumber = response.total;
        if (this.noReplyNumber != 0) {
          this.$refs.audio1.currentTime = 0; //从头开始播放提示音
          this.$refs.audio1.play(); //播放
        }
      });
    },
    /** 匿名按钮 */
    handleAnonymous() {
      this.loading = true;
      console.log(this.count);
      if (this.count != 1) {
        this.count = this.count + 1;
        this.getNoAnonymous();
      } else {
        this.count = 0;
        this.getAnonymous();
      }
      console.log(this.count);
    },
    getAnonymous(){
      getAnonymous(this.queryParams).then((response) => {
          this.complainList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
    },
    getNoAnonymous(){
      getNoAnonymous(this.queryParams).then((response) => {
          this.complainList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.opens = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        complainId: null,
        anonymous: null,
        companyId: null,
        complainantPhone: null,
        complainTime: null,
        respondent: null,
        complainTitle: null,
        complainContent: null,
        userId: null,
        nickName: null,
        replyContent: null,
        fakeDelete: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.complainId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增投诉";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.audio2.currentTime = 0;
      this.$refs.audio2.play();
      this.reset();
      const complainId = row.complainId || this.ids;
      getComplain(complainId).then((response) => {
        this.form = response.data;
        this.opens = true;
        this.title = "回复投诉";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        console.log(this.form);
        if (valid) {
          if (this.form.complainId != null) {
            updateComplain(this.form).then((response) => {
              this.$modal.msgSuccess("该投诉已回复");
              this.opens = false;
              this.getList();
              this.handle();
            });
          } else {
            addComplain(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.handle();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$refs.audio3.currentTime = 0;
      this.$refs.audio3.play();
      const complainIds = row.complainId || this.ids;
      this.$modal
        .confirm('是否确认删除序号为"' + complainIds + '"的投诉？')
        .then(() => {
          delComplain(complainIds).then((res) => {
            console.log("////" + res);
            if (res != "101") {
              this.getList();
              this.$modal.msgSuccess("该条投诉已成功删除");
            } else {
              this.$modal.msgWarning("该条投诉未回复，不可删除");
            }
          });
        })
        .catch(() => {});
    },
    /** 查看详情 */
    handleLookOver(row) {
      this.$refs.audio2.currentTime = 0;
      this.$refs.audio2.play();
      getComplain(row.complainId).then((response) => {
        this.form = response.data;
        if (this.form.anonymous == 1) {
          this.form.companyName = "********" + "(该公司已匿名)";
        }
        this.lookOver = true;
        this.title = "查看投诉详情";
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/complain/export",
        {
          ...this.queryParams,
        },
        `complain_${new Date().getTime()}.xlsx`,
        (this.$refs.audio4.currentTime = 0),
        this.$refs.audio4.play()
      );
    },
  },
};
</script>
